package net.zjitc.controller;


import net.zjitc.pojp.CateGory;
import net.zjitc.pojp.Evaluate;
import net.zjitc.pojp.Product;
import net.zjitc.service.AdminCateGorySecondService;
import net.zjitc.service.AdminCateGoryService;
import net.zjitc.service.AdminProductService;
import net.zjitc.service.UserService;
import net.zjitc.service.impl.AdminCateGorySecondServiceImpl;
import net.zjitc.service.impl.AdminCateGoryServiceImpl;
import net.zjitc.service.impl.AdminProductServiceImpl;
import net.zjitc.service.impl.UserServiceImpl;
import net.zjitc.utils.PageBean;
import net.zjitc.utils.dispacher.URLMapping;
import org.apache.log4j.Category;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by lenovo on 2017/12/13.
 */
public class UserProduct {
    AdminCateGoryService service=new AdminCateGoryServiceImpl();
    AdminCateGorySecondService secondService=new AdminCateGorySecondServiceImpl();
    AdminProductService productService=new AdminProductServiceImpl();
    UserService userService=new UserServiceImpl();
    int pageSize=12;
    //通过一级分类，页面显示的左边需要一级分类与二级分类名称
    @URLMapping(url = "/findByCid")
    public String findByCid(HttpServletRequest request, HttpServletResponse response) {
        int page = Integer.valueOf(request.getParameter("page"));
        int cid = Integer.valueOf(request.getParameter("cid"));

        List<CateGory> clist = service.findOneList();
        request.setAttribute("clist",clist);

        //准备一级分类下的所有产品 ，按分页
        PageBean<Product> pageBean = productService.findPageBeanByCid(page,pageSize,cid);
        request.setAttribute("page",pageBean);

         /*一级分类的cid，用于分页查询用*/
        request.setAttribute("cid",cid);
        return "/WEB-INF/user/productList.jsp";
    }

    /**
     通过二级分类查询产品，页面显示的左边需要一级分类与二级分类名称
     为什么准备两个页面？？
     因为页面有分页功能，根据页码进行查询的时候，
     分类查询参数是一级分类或者二级分类，它们是不一样的，所有用两个页面做区分，但是页面显示效果基本一样
     */
    @URLMapping(url = "/findByCsid")
    public String findByCsid(HttpServletRequest request, HttpServletResponse response) {
        //左则栏中的数据
        List<CateGory> clist = service.findOneList();
        request.setAttribute("clist",clist);

        int page = Integer.valueOf(request.getParameter("page"));
        int csid = Integer.valueOf(request.getParameter("csid"));

        PageBean<Product> pageBean = productService.findPageBeanByCsid(page,pageSize,csid);
        request.setAttribute("page",pageBean);

        request.setAttribute("csid",csid);
        return "WEB-INF/user/product_secondList.jsp";
    }
    /**
     * 商品详细信息显示
     * */
    @URLMapping(url = "/findByPid")
    public String findByPid(HttpServletRequest request, HttpServletResponse response){

        int pid = Integer.valueOf(request.getParameter("pid"));

        //左则栏中的数据
        List<CateGory> clist = service.findOneList();
        request.setAttribute("clist",clist);
        //商品数据准备
        Product product = productService.findByPid(pid);
        request.setAttribute("p",product);

        List<Evaluate> evaluates = productService.findEvaluateByPid(pid);
        request.setAttribute("Evaluate",evaluates);
        return "WEB-INF/user/product.jsp";
    }
































}
